package demo.util;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 
 * @author caohj
 * @Date 2017年8月2日
 */
public class NamedThreadFactory implements ThreadFactory {

	private String threadName;
	/** 守护线程 */
	private boolean deamon;
	private AtomicInteger threadNo = new AtomicInteger();

	public NamedThreadFactory(String threadName, boolean deamon) {
		this.threadName = threadName == null ? "NoName" : threadName;
		this.deamon = deamon;
	}

	@Override
	public Thread newThread(Runnable r) {
		Thread thread = new Thread(r, threadName + threadNo.incrementAndGet());
		thread.setDaemon(deamon);
		return thread;
	}

}
